package cn.lzj.controller.User;

import cn.lzj.Utils.AliSmsUtil;
import cn.lzj.Utils.GetImgUtils;
import cn.lzj.Utils.ReturnInfoUtils;
import cn.lzj.pojo.User;
import cn.lzj.service.User.UserInfoService;
import cn.lzj.service.User.UserService;
import cn.lzj.service.base.RegisterService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


//用户个人信息controller
@Slf4j
@Controller
@RequestMapping("/userInfo")
public class UserInfoController {

    @Resource
    private UserInfoService userInfoService;

    @Resource
    private RegisterService registerService;


    @Resource
    private RedisTemplate<String,String> redisTemplate;

    String code = "";

    //获取个人的信息
    @GetMapping("/ownerInfo")
    public String ownerInfo(){
        return "ownerInfo";
    }

    @PostMapping("/modifyHeadImg")
    public String modifyHeadImg(@RequestParam("myFile") MultipartFile myFile,
                                @RequestParam("userid") Integer userId,
                                HttpServletRequest request
                                ){
        log.info("上传文件");
        User user = userInfoService.selectUserById(userId);
        boolean success = true;
        String path = "D:\\myImg";

        String fileName = GetImgUtils.getImg(myFile, path);
        success = userInfoService.modifyHeadImg(userId,fileName);
        user.setUserImg(fileName);
        HttpSession session = request.getSession();
        session.setAttribute("user",user);
        return "ownerInfo";
    }


    @PostMapping("/modifyEmail")
    public String modifyEmail(@RequestParam("userid") Integer userId,
                              String email,
                              String check,
                              String verifyCode
                              ){
//        String defaultKaptchaEmail = (String) httpServletRequest.getSession().getAttribute("verifyCodeEmail");

        BoundValueOperations<String, String> opsEmail = redisTemplate.boundValueOps("verifyCodeEmail");
        String defaultKaptchaEmail = opsEmail.get();

        if(code.equals(check) && defaultKaptchaEmail.equals(verifyCode)){

            boolean success = userInfoService.modifyEmail(userId,email);
            if(success){
                return "ownerInfo";
            }else {
                return "error";
            }
        }else {
            return "error";
        }
    }

    @PostMapping("/modifyPhoneNo")
    public String modifyPhoneNo(@RequestParam("userid") Integer userId,
                                String newPhone,
                                String check,
                                String verifyCode
                                ){
//        String verifyCodePhone = (String) httpServletRequest.getSession().getAttribute("verifyCodePhone");

        BoundValueOperations<String, String> opsEmail = redisTemplate.boundValueOps("verifyCodePhone");
        String defaultKaptchaPhone = opsEmail.get();

        if(code.equals(check) && defaultKaptchaPhone.equals(verifyCode)){
            boolean success = userInfoService.modifyPhoneNo(userId,newPhone);
            if(success){
                return "ownerInfo";
            }else {
                return "error";
            }
        }else {
            return "error";
        }
    }


    //http://localhost:8080/userInfo/ownerInfo/getCodeNum/18377731569
    @GetMapping("/getCodeNum/{phone}")
    @ResponseBody
    public void infoGetNum(@PathVariable("phone") String phone){
        code = registerService.getCodeNum(phone);
        System.out.println(code);
        AliSmsUtil.sendSmsCode(phone,code);
    }


    @PostMapping("/modifyPassword")
    public String modifyPassword(HttpServletRequest request,Model model){
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");

        String codeUser = request.getParameter("code");
        String newPassword = request.getParameter("newPassword");
        String verifyCode = request.getParameter("verifyCode");
        if(verifyCode.equals("") && code.equals(codeUser)){
            user.setPassword(newPassword);
            userInfoService.modifyPassword(user);
            return "login";
        }
        return "error";
    }
}
